﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace MvcEngine.Mvc.View.Controls
{
    public static class PanelExtensions
    {
        public static CollapsablePanel BeginCollapsablePanel(this HtmlHelper htmlHelper, string header)
        {
            return BeginCollapsablePanel(htmlHelper, header, true);
        }

        public static CollapsablePanel BeginCollapsablePanel(this HtmlHelper htmlHelper, string header, bool collapsed)
        {
            return CollapsablePanelHelper(htmlHelper, header, collapsed, null);
        }

        public static CollapsablePanel BeginCollapsablePanel(this HtmlHelper htmlHelper, string header, bool collapsed, string info)
        {
            return CollapsablePanelHelper(htmlHelper, header, collapsed, info);
        }

        public static void EndCollapsablePanel(this HtmlHelper htmlHelper)
        {
            htmlHelper.ViewContext.HttpContext.Response.Write("</div></div>");
        }

        private static CollapsablePanel CollapsablePanelHelper(this HtmlHelper htmlHelper, string header, bool collapsed, string info)
        {
            TagBuilder panelBuilder = new TagBuilder("div");
            panelBuilder.AddCssClass("collapsablePanel");

            htmlHelper.ViewContext.HttpContext.Response.Write(panelBuilder.ToString(TagRenderMode.StartTag));

            TagBuilder imageDivBuilder = new TagBuilder("div");
            if (collapsed)
                imageDivBuilder.AddCssClass("collapsablePanelCollapsedImage");
            else
                imageDivBuilder.AddCssClass("collapsablePanelExpandedImage");

            TagBuilder imageContainerBuilder = new TagBuilder("div");
            imageContainerBuilder.AddCssClass("collapsablePanelImageContainer");
            imageContainerBuilder.InnerHtml = imageDivBuilder.ToString(TagRenderMode.Normal);

            htmlHelper.ViewContext.HttpContext.Response.Write(imageContainerBuilder.ToString(TagRenderMode.Normal));

            TagBuilder headerBuilder = new TagBuilder("div");
            headerBuilder.AddCssClass("collapsablePanelHeader");
            headerBuilder.InnerHtml = htmlHelper.ViewContext.HttpContext.Server.HtmlEncode(header);
            htmlHelper.ViewContext.HttpContext.Response.Write(headerBuilder.ToString(TagRenderMode.Normal));

            if (!string.IsNullOrEmpty(info)) 
            {
                TagBuilder infoBuilder = new TagBuilder("div");
                infoBuilder.AddCssClass("collapsablePanelInfo");
                infoBuilder.InnerHtml = htmlHelper.ViewContext.HttpContext.Server.HtmlEncode(info);
                htmlHelper.ViewContext.HttpContext.Response.Write(infoBuilder.ToString(TagRenderMode.Normal));            
            }

            TagBuilder contentBuilder = new TagBuilder("div");
            contentBuilder.AddCssClass("collapsablePanelContent");
            if (collapsed)
                contentBuilder.Attributes.Add("style", "display:none");
            else
                contentBuilder.Attributes.Add("style", "display:block");

            htmlHelper.ViewContext.HttpContext.Response.Write(contentBuilder.ToString(TagRenderMode.StartTag));

            return new CollapsablePanel(htmlHelper.ViewContext.HttpContext.Response);
        }
    }
}
